package com.briup.smartcity_group6.web.controller;


import com.briup.smartcity_group6.bean.basic.BaseUser;
import com.briup.smartcity_group6.service.IBaseUserService;
import com.briup.smartcity_group6.utils.Result;
import com.briup.smartcity_group6.utils.ResultUtil;
import com.briup.smartcity_group6.web.vo.RegisterUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.servlet.http.HttpServletRequest;
import java.awt.*;

@Api(tags = "登录相关模块")
@RestController
public class LoginController {
    @Autowired
    private IBaseUserService service;

    @ApiOperation("用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true),
            @ApiImplicitParam(name = "password",value = "密码",required = true)
    })
    @PostMapping(value = "/login",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result login(String username, String password){
        //1.调用service层方法，传递参数
        String token = service.login(username, password);
        return ResultUtil.success(token);
    }
    @ApiOperation("新增用户信息")
    @PostMapping(value = "/register",consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result register(RegisterUserVO vo){
        //1.将接收的参数信息传递service层
        BaseUser baseUser = new BaseUser();
        BeanUtils.copyProperties(vo,baseUser);
        service.addUser(baseUser);
        return ResultUtil.success("注册成功,请登录");
    }
    @ApiOperation("用户退出")
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request){
        /*
         * 退出登录的功能：目前只要实现返回正确的字符串格式。
         * 如何要实现，我们需要做什么事情。(了解)
         *  1.可以清除系统中缓存的用户相关信息
         *  2.可以修改用户登录状态 * */


        return ResultUtil.success("退出成功");


    }
}
